package edu.nwpu.service.impl;

import edu.nwpu.annotation.MyServiceAnnotation;
import edu.nwpu.dao.FoodMapper;
import edu.nwpu.dao.FoodTypeMapper;
import edu.nwpu.pojo.Food;
import edu.nwpu.service.FoodService;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("foodService")
public class FoodServiceImpl implements FoodService {

    @Resource(name = "foodMapper")
    private FoodMapper foodMapper;

    @Resource
    private FoodTypeMapper foodTypeMapper;

    @Override
    public Food getFoodById(Long id) {
        return foodMapper.selectFoodByFoodId(id);
    }

    @Override
    public List<Food> getFoodsByLikeName(String name) {
        return foodMapper.selectFoodsByLikeName(name);
    }

    @Override
    public List<Food> getFoodsPage(Integer offset, Integer limit) {
        return foodMapper.selectFoodListPage(offset, limit);
    }

    @Override
    public List<Food> getFoodsByTypeId(Long typeId) {
        return foodMapper.selectFoodsByTypeId(typeId);
    }

    @Override
    public List<Food> getAllFoods() {
        return foodMapper.getAllFoods();
    }

    @Override
    public boolean addFood(Food food) {
        // TODO food的图片暂时未处理
        if (food == null || "".equals(food.getName()) || "".equals(food.getPrice())) {
            return false;
        } else if (foodMapper.getFoodByName(food.getName()) != null) {
            // 菜品名称冲突
            return false;
        } else {
            long ret = foodMapper.addFood(food);
            return ret > 0;
        }
    }

    @Override
    public boolean updateFood(Food food) {
        // TODO food的图片暂时未处理
        if (food == null || "".equals(food.getName()) || "".equals(food.getPrice())) {
            return false;
        } else if (foodMapper.getFoodByName(food.getName()) != null) {
            // 菜品名称冲突
            return false;
        } else {
            long ret = foodMapper.updateFood(food);
            return ret > 0;
        }
    }

    @Override
    @MyServiceAnnotation
    public boolean deleteFoodById(Long id) {
        int ret = 0;
        try {
            ret = foodMapper.deleteFoodById(id);
        } catch (DataIntegrityViolationException e) {
            return false;
        } catch (Exception e) {
            return false;
        }
        return ret > 0;
    }


    /**
     * 中间表关联添加
     *
     * @param typeId
     * @param foodId
     * @return
     */
    @Deprecated
    public boolean addFoodAndFoodType(Long typeId, Long foodId) {
        if (typeId == null || foodId == null) {
            return false;
        }
        return foodMapper.addFoodAndFoodType(typeId, foodId) > 0;
    }

    /**
     * 根据typeId和foodId，中间表关联删除
     *
     * @param typeId
     * @param foodId
     * @return
     */
    @Deprecated
    public boolean deleteFoodAndType(Long typeId, Long foodId) {
        if (typeId == null || foodId == null) {
            return false;
        }
        return foodMapper.deleteFoodAndType(typeId, foodId) > 0;
    }

    /**
     * 根据typeId，中间表关联删除
     *
     * @param typeId
     * @return
     */
    @Deprecated
    public boolean deleteFoodAndTypeByTypeId(Long typeId) {
        if (typeId == null) {
            return false;
        }
        List<Food> foods = foodTypeMapper.getFoodsById(typeId);
        // 删除菜品类别下的菜品时如果正有处理中的order在使用其中的菜品时，由于外键依赖，无法删除对应菜品，异常需要捕获
        return foodMapper.deleteFoodAndTypeByTypeId(typeId) == foods.size();
    }
}
